package com.example.rabbitmq.demo5_lazy_mode;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @Author songyahui
 * @Date 2023/7/13 14:59
 * @Description
 */
@RestController
@RequestMapping("/lazyMode")
public class LazyModeController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // lazyMode队列的可执行测试
    @GetMapping("/sendMsg/{message}")
    private String sendMsg(@PathVariable("message") String message){
        //obj参数如果是非Message类型实例则默认执行消息持久化
        rabbitTemplate.convertAndSend("","lazy_mode_queue",message);
        return "发布成功！！！";
    }

    // lazyMode队列消息是否可持久化测试
    @GetMapping("/sendMsg2/{message}")
    private String sendMsg2(@PathVariable("message") String message){
        //obj参数设置Message类型实例，交付模式为非持久化。如果mq宕机恢复后消息依然存在则说明lazyMode队列确实是做了消息的持久化
        //模拟流程：
//            1.关闭消费者LazyModeConsumer（测试结束后恢复开启）
//            2.执行接口
//            3.查看控制台，是否有待消费消息
//            4.如果有，stop mq服务
//            5.start mq服务，观察待消费消息是否依然存在
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        Message msg =  new Message((message).getBytes(StandardCharsets.UTF_8), messageProperties);
        rabbitTemplate.convertAndSend("","lazy_mode_queue",msg.getBody());
        return "发布成功！！！";
    }
}